const express = require('express')
const router = express.Router()
const multer = require('multer')

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        // 指定文件路径
        cb(null, './public/upload/image')
    },
    filename: function (req, file, cb) {
        // 指定文件名
        var fileFormat = (file.originalname).split('.')
        cb(null, file.fieldname + '-' + Date.now() + '.' + fileFormat[fileFormat.length - 1])
    }
})

var upload = multer({ storage: storage })
var uploads = multer({ storage: storage }).array('images')

/**
 * @api {post} /file/uploadImg 上传图片（返回图片的服务器地址）
 * @apiName upload
 * @apiGroup file
 *
 * @apiParam {file} name 图片.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/upload', upload.single('fileImg'), (req, res) => {
    let { size, mimetype, path, filename } = req.file
    let types = ['jpg', 'jpeg', 'png', 'gif']  //允许上传的数据类型
    let tmpType = mimetype.split('/')[1]
    if (size > 10000000) {
        return res.send({ err: -1, msg: '尺寸过大,上传图片最大为10MB' })
    } else if (types.indexOf(tmpType) == -1) {
        return res.send({ err: -2, msg: '媒体类型错误' })
    } else {
        let url = `/upload/image/${filename}`
        res.send({ err: 0, msg: '上传成功！', img: url })
    }
})


/**
 * @api {post} /file/uploads 上传多图片（返回图片的服务器地址）
 * @apiName upload
 * @apiGroup file
 *
 * @apiParam {file} fileImg 图片集合.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/uploads', upload.array('fileImg', 10), (req, res) => {
    let types = ['jpg', 'jpeg', 'png', 'gif']  //允许上传的数据类型
    let files = req.files;
    console.log(req.files)
    if (files.length === 0) {
        res.send({ err: -1, msg: '上传文件不能为空' })
        return
    } else {
        let fileInfos = [];
        for (var i in files) {
            let file = files[i];
            let fileInfo = {};


            // fs.renameSync('./upload/' + file.filename, './upload/' + file.originalname);//这里修改文件名。

            //获取文件基本信息
            fileInfo.mimetype = file.mimetype;
            fileInfo.originalname = file.originalname;
            fileInfo.size = file.size;
            fileInfo.path = file.path;

            let tmpType = fileInfo.mimetype.split('/')[1]
            if (fileInfo.size > 10000000) {
                return res.send({ err: -1, msg: '图' + i + '尺寸过大,上传图片最大为10MB' })
            } else if (types.indexOf(tmpType) == -1) {
                return res.send({ err: -2, msg: '图' + i + '媒体类型错误' })
            } else {
                let url = fileInfo.path.split('public\\')[1]
                fileInfo.imgUrl = url
                // res.send({ err: 0, msg: '上传成功！', img: url })
            }

            fileInfos.push(fileInfo);
            
        }
        // console.log(fileInfos)
        // 设置响应类型及编码
        res.send({ err: 0, msg: '上传成功aaa', data: fileInfos })
    }
})



module.exports = router